//
//  PullTableView.m
//  UniversalApp
//
//  Created by 胡双喜 on 2018/6/29.
//  Copyright © 2018年 邓超. All rights reserved.
//

#import "PullTableView.h"
#import "YHRefreshHeader.h"
@implementation PullTableView
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        self.refreshingEnabled = YES;
        self.loadMoreingEnabled = YES;
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if(self.contentSize.height < self.height)
    {
        self.contentSize = CGSizeMake(self.contentSize.width, self.height + 1);
    }
}


- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if(self)
    {
        self.refreshingEnabled = YES;
        self.loadMoreingEnabled = YES;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.refreshingEnabled = YES;
        self.loadMoreingEnabled = YES;
    }
    return self;
}

- (void)beginRefreshing
{
    [self.mj_header beginRefreshing];
}

- (void)endRefreshing
{
    [self.mj_header endRefreshing];
}

- (void)endLoadMoreing
{
    [self.mj_footer endRefreshing];
}

- (void)endRefreshingWithNoMoreData
{
    [self.mj_footer endRefreshingWithNoMoreData];
}

- (void)setRefreshingEnabled:(BOOL)refreshingEnabled
{
    _refreshingEnabled = refreshingEnabled;
    
    if(refreshingEnabled)
    {
        @weakify(self)
        YHRefreshHeader *header = [YHRefreshHeader headerWithRefreshingBlock:^{
            @strongify(self)
            if(self.pullDelegate && [self.pullDelegate respondsToSelector:@selector(tableViewWillRefresh:)])
            {
                [self.pullDelegate tableViewWillRefresh:self];
            }
        }];
        self.mj_header = header;
    }
    else
    {
        self.mj_header = nil;
    }
    
}

- (void)setLoadMoreingEnabled:(BOOL)loadMoreingEnabled
{
    _loadMoreingEnabled = loadMoreingEnabled;
    if(loadMoreingEnabled)
    {
        @weakify(self)
        MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            @strongify(self)
            if(self.pullDelegate && [self.pullDelegate respondsToSelector:@selector(tableViewWillLoadMore:)])
            {
                [self.pullDelegate tableViewWillLoadMore:self];
            }
        }];
        //        footer.automaticallyRefresh = NO;
        self.mj_footer = footer;
    }
    else
    {
        self.mj_footer = nil;
    }
}

- (BOOL)allowsHeaderViewsToFloat
{
    return self.allowHeaderFloat;
}

- (BOOL)allowsFooterViewsToFloat {
    return self.allowFooterFloat;
}

@end

